package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:分类管理
 * @author: yanhongwei
 * @date: 2023/5/19 17:09
 * @param:
 * @return:
 **/
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * @description:新增分类
     * @author: yanhongwei
     * @date: 2023/5/20 8:41
     * @param: [category]
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("category:{}", category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * @description:分页查询
     * @author: yanhongwei
     * @date: 2023/5/20 8:40
     * @param: [page, pageSize]
     * @return: com.itheima.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     **/
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {

        Page page1 = new Page(page, pageSize);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(page1,queryWrapper);

        return R.success(page1);
    }

    /**
     * @description:根据id删除分类
     * @author: yanhongwei
     * @date: 2023/5/20 11:52
     * @param: [id]
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类,ids为:{}",ids);

        categoryService.remove(ids);

        return R.success("分类信息删除成功");
    }

    /**
     * @description:根据id修改分类
     * @author: yanhongwei
     * @date: 2023/5/22 9:51
     * @param: [category]
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @PutMapping
    public R<String> update(@RequestBody Category category){

        log.info("修改分类,category为:{}"+category.toString());


        categoryService.updateById(category);

        return R.success("修改分类成功");
    }

    /**
     * @description:根据条件查询分类数据
     * @author: yanhongwei
     * @date: 2023/5/23 11:04
     * @param: [category]
     * @return: com.itheima.reggie.common.R<java.util.List<com.itheima.reggie.entity.Category>>
     **/
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
